package com.example.administrator.im.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.im.R;
import com.example.administrator.im.db.ContactInvitedDBHelper;
import com.example.administrator.im.utils.DialogUtils;
import com.example.administrator.im.utils.HttpUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * create:2018/10/3 19:20
 * author: 姚鸿杰
 **/
public class SystemNotifycationAdapter extends RecyclerView.Adapter<SystemNotifycationAdapter.Holder> {

    private final String TAG = "SystemAdapter";

    private List<ContactInvitedDBHelper> mList;
    private ContactInvitedDBHelper contact;
    private Context mContext;

    public SystemNotifycationAdapter(List<ContactInvitedDBHelper> mList, Context mContext) {
        if (mList == null) {
            this.mList = new ArrayList<>();
        } else {
            this.mList = mList;
        }
        this.mContext = mContext;
    }

    public void addItemInFirst(ContactInvitedDBHelper contactInvited) {
        if (contactInvited != null && mList != null) {
            mList.add(0, contactInvited);
            notifyItemInserted(0);
            notifyItemRangeChanged(0, mList.size());
        }
    }


    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(mContext).
                inflate(R.layout.recycler_systen_notifycation, viewGroup, false);
        return new Holder(v);
    }

    private void setResult(ContactInvitedDBHelper contact, int result) {
        contact.setResult(result);
        contact.setTime(System.currentTimeMillis());
        contact.save();
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, int i) {

        contact = mList.get(i);

        switch (contact.getResult()) {
            case 0: //未执行

                //拒绝
                holder.asn_refuse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.setDialog(mContext, R.layout.loading);
                        DialogUtils.showDialog();
                        holder.asn_refuse.setClickable(false);

                        try {
                            EMClient.getInstance().contactManager().declineInvitation(contact.getUsername());
                            setResult(contact, -1);
                        } catch (HyphenateException e) {
                            e.printStackTrace();
                        }
                    }
                });
                //同意
                holder.agree.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.setDialog(mContext, R.layout.loading);
                        DialogUtils.showDialog();
                        holder.asn_refuse.setClickable(false);
                        try {//同意好友邀请
                            EMClient.getInstance().contactManager().acceptInvitation(contact.getUsername());
                            setResult(contact, 1);
                        } catch (HyphenateException e) {
                            e.printStackTrace();
                        }
                    }
                });

                break;
            case 1:
                holder.received.setText("已同意");
                holder.agree.setVisibility(View.GONE);
                holder.asn_refuse.setVisibility(View.GONE);
                break;
            case -1:
                holder.received.setText("已拒绝");
                holder.agree.setVisibility(View.GONE);
                holder.asn_refuse.setVisibility(View.GONE);
                break;
        }

        String url = HttpUtils.PORT + contact.getImg_path();
        if (!TextUtils.equals(url, HttpUtils.PORT)) {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.login_logo)
                    .error(R.mipmap.login_logo)
                    .override(150, 150);
            Glide.with(mContext).load(url).apply(options).into(holder.asn_img);
        }

        holder.asn_nickname.setText(contact.getNickname());
        holder.asn_username.setText(contact.getUsername());
        holder.asn_reason.setText(contact.getReason());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class Holder extends RecyclerView.ViewHolder {


        private TextView received, asn_nickname, asn_username, asn_reason;
        private CircleImageView asn_img;
        private Button agree, asn_refuse;


        public Holder(@NonNull View itemView) {
            super(itemView);

            received = (TextView) itemView.findViewById(R.id.received);
            asn_nickname = (TextView) itemView.findViewById(R.id.asn_nickname);
            asn_username = (TextView) itemView.findViewById(R.id.asn_username);
            asn_reason = (TextView) itemView.findViewById(R.id.asn_reason);

            asn_img = (CircleImageView) itemView.findViewById(R.id.asn_img);
            agree = (Button) itemView.findViewById(R.id.agree);
            asn_refuse = (Button) itemView.findViewById(R.id.asn_refuse);

        }
    }
}
